package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.TopicNotice;
import com.rjwh.dingdong.client.util.PicPathUtil;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.view.EmoticonsTextView;
import com.rjwh.dingdong.client.widget.RoundImageView;
import com.wby.base.AdapterBase;

/**
 * 社区消息列表
 *
 * @author 王小珂
 * @ClassName CommunityPostMsgAdapter
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-4-14 下午4:48:51
 * @history 1.YYYY-MM-DD author: description:
 */
public class CommunityPostMsgAdapter extends AdapterBase<TopicNotice> implements ListAdapter {
    private LayoutInflater inflater;
    private Context mContext;

    public CommunityPostMsgAdapter(Context context) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    protected View getExView(int arg0, View arg1, ViewGroup arg2) throws Exception {
        if (arg1 == null) {
            arg1 = inflater.inflate(R.layout.community_post_msg_item, null);
        }
        RoundImageView headIV = ViewHolder.get(arg1, R.id.item_community_post_msg_head_iv);
        TextView name_TV = ViewHolder.get(arg1, R.id.item_community_post_msg_user_name_tv);
        TextView timeTV = ViewHolder.get(arg1, R.id.item_community_post_msg_time);
        ImageView pointIV = ViewHolder.get(arg1, R.id.item_community_post_msg_alert_point);
        TextView contentTV = ViewHolder.get(arg1, R.id.item_community_post_msg_content);

        TopicNotice topicNotice = mList.get(arg0);
        if (topicNotice == null) {
            return arg1;
        }

        if (!AsStrUtils.isEmpty(topicNotice.getUsername())) {
            name_TV.setText(topicNotice.getUsername());
        }
        if (!AsStrUtils.isEmpty(topicNotice.getTime())) {
            timeTV.setText(topicNotice.getTime());
        }
        if (!AsStrUtils.isEmpty(topicNotice.getIsnew()) && topicNotice.getIsnew().equals("1")) {
            pointIV.setVisibility(View.VISIBLE);
        } else {
            pointIV.setVisibility(View.GONE);
        }

        String content = topicNotice.getContent();
        if (!AsStrUtils.isEmpty(content)) {
            /**
             * 给自定义的TextView设置不同位子的字体颜色
             */
            SpannableString ss = new SpannableString(content);
            ss.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.content_text_color)), 0, content.indexOf("#"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.blue_button_nor)), content.indexOf("#"), content.lastIndexOf("#") + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.content_text_color)), content.lastIndexOf("#") + 1, content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            contentTV.setText(ss);
        }
        if (!AsStrUtils.isEmpty(topicNotice.getAvatar())) {
            Glide.with(mContext).load(PicPathUtil.getPicUrl(topicNotice.getAvatar())).placeholder(R.drawable.icon_defult_avatar).error(R.drawable.icon_defult_avatar).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(headIV);
        }

        return arg1;
    }

    @Override
    protected void onReachBottom() {

    }

}
